﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AlgoritmTeorija
{
    public partial class InsertionSort : Form
    {

        protected string FileName = "Data.txt";
        private BackgroundWorker _bw = new BackgroundWorker();

        public InsertionSort()
        {
            InitializeComponent();
            _bw.DoWork += bw_DoWork;
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            if(rdFile.Checked)
                doInsertSort();
            else
            {
                doInsertSortRAM();
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            DisplayData(FileName);
            _bw.RunWorkerAsync();
        }

        private void btnDisplayData_Click(object sender, EventArgs e)
        {
            DisplayData(FileName);
        }

        private void btnRandom_Click(object sender, EventArgs e)
        {
            RandomDataHelper rd = new RandomDataHelper(FileName);
            rd.GenerateRandomNumbers(Convert.ToInt32(txtQuantity.Text));
            DisplayData(FileName);

        }

        private void DisplayData(string fileName)
        {
            FileHelper fh = new FileHelper(fileName, FileMode.Open);
            txtData.Text = fh.GetAllFile();
        }

        private void txtQuantity_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }


        #region INSERT SORT
        private void doInsertSort()
        {
            FileHelper fh = new FileHelper(FileName, FileMode.Open);
            fh.CopyFile(FileName);
            int i, j;
            progressBar.Invoke((MethodInvoker) delegate{progressBar.Maximum = fh.Length;}); // atnaujiname progress bar'ą
            TimeHelper th = new TimeHelper();
            th.StartTimer(); // pradedame skaičiuoti laiką

            for (i = 1; i < fh.Length; i++)
            {
                int value = fh.ReadBinary(i);
                j = i - 1;
                while ((j >= 0) && (fh.ReadBinary(j).CompareTo(value) > 0))
                {
                    fh.WriteBinary((j + 1),fh.ReadBinary(j));
                    j--;
                }
                fh.WriteBinary(j + 1, value);
                progressBar.Invoke((MethodInvoker)delegate { progressBar.Value = i; }); // atnaujiname progress bar'ą
            }


            th.StopTimer();
            txtResult.Text = fh.GetAllFile(); // išspausdiname rezultata į langą.
            MessageBox.Show("Veikimo laikas: " + th.GetDuration().ToString() + "\r\nOperacijų skaičius: " + fh.opCount,
                            "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void doInsertSortRAM()
        {
            int[] list = null;
            string[] tempList = null;
            FileHelper fh = new FileHelper(FileName, FileMode.Open);
            fh.CopyFile(FileName);
            int i, j;
            tempList = fh.GetAllFile().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            progressBar.Invoke((MethodInvoker) delegate { progressBar.Maximum = tempList.Length; });
            // atnaujiname progress bar'ą
            list = new int[tempList.Length];
            int currIndex = 0;
            foreach (string str in tempList)
            {
                list[currIndex] = Convert.ToInt32(str);
                currIndex++;
            }
            TimeHelper th = new TimeHelper();
            th.StartTimer(); // pradedame skaičiuoti laiką

            for (i = 1; i < list.Length; i++)
            {
                int value = list[i];
                j = i - 1;
                while ((j >= 0) && (list[j].CompareTo(value) > 0))
                {
                    list[j + 1] = list[j];
                    j--;
                }
                list[j + 1] = value;
                progressBar.Invoke((MethodInvoker) delegate { progressBar.Value = i; }); // atnaujiname progress bar'ą
            }


            th.StopTimer();
            StringBuilder builder = new StringBuilder();
            Array.ForEach(list, x => builder.Append(x + "\r\n"));
            txtResult.Text = builder.ToString();

            // išspausdiname rezultata į langą.
            MessageBox.Show("Veikimo laikas: " + th.GetDuration().ToString(),
                            "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        #endregion


    }
}
